﻿using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Cms.Web.Framework.Controls
{
    public class AdminTabStrip : Control, ICallbackEventHandler
    {
        #region Delegates

        public delegate void ChangeHandler(string value);

        #endregion

        public string SelectedValue
        {
            get
            {
                return ViewState["SelectedValue"] as string;
            }
            set
            {
                ViewState["SelectedValue"] = value;
                if (CurrentValue == null)
                    CurrentValue = value;
            }
        }
        
        public string CurrentValue
        {
            get { return ViewState["CurrentValue"] as string; }
            internal set { ViewState["CurrentValue"] = value; }
        }

        #region ICallbackEventHandler Members

        public string GetCallbackResult()
        {
            return string.Empty;
        }
        
        public void RaiseCallbackEvent(string eventArgument)
        {
        }

        #endregion

        public event ChangeHandler Change;
        
        internal void SetSelectedValue(string newValue)
        {
            CurrentValue = SelectedValue;
            SelectedValue = newValue;
        }
        
        internal void RaiseOnChange()
        {
            if (Change == null)
                return;
            Change(SelectedValue);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            CurrentValue = SelectedValue;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!Page.IsValid)
                SelectedValue = CurrentValue;
            Control container;
            if (Page.Master != null)
                container = Page.Master;
            else
                container = Page;
            (container.FindControl("mainBoxTopOuterContainer") as HtmlGenericControl).Attributes.Add("class", "tabs");
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.WriteBeginTag("div");
            writer.WriteAttribute("class", "mainTabs");
            writer.Write("><ul>");
            base.Render(writer);
            writer.Write("</ul>");
            writer.WriteEndTag("div");
        }
    }
}